package com.ider.deskcust;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;

/**
 * Created by ider-eric on 2016/5/23.
 * 此类实现了可序列化Parcelable接口
 */
public class PageEntry implements Parcelable {

    public Theme theme;
    public TileEntry[] tileEntrys;



    public PageEntry() {
    }

    public PageEntry(JSONObject json) {
        try {
            theme = new Theme(json.getJSONObject("theme"));
            JSONArray jarray = json.getJSONArray("stru");
            tileEntrys = new TileEntry[jarray.length()];
            for(int i = 0;i<jarray.length();i++){
                tileEntrys[i]=new TileEntry(jarray.getJSONObject(i));
            }
//            for(int i = 0;i<jarray.length();i++){
//                tileEntrys[i+2]=new TileEntry(jarray.getJSONObject(i));
//            }
//            for(int i = 0;i<jarray.length();i++){
//                tileEntrys[i+12]=new TileEntry(jarray.getJSONObject(i));
//            }
//            for(int i = 0;i<jarray.length();i++){
//                tileEntrys[i+12]=new TileEntry(jarray.getJSONObject(i));
//            }
        } catch (JSONException e) {
            tileEntrys = new TileEntry[0];
            e.printStackTrace();
        }
    }
    @Override
    public void writeToParcel(Parcel parcel, int flag) {

        parcel.writeParcelable(theme, flag);
        parcel.writeParcelableArray(tileEntrys, flag);
    }


    public static final Creator<PageEntry> CREATOR = new Creator<PageEntry>() {
        @Override
        public PageEntry createFromParcel(Parcel parcel) {
            PageEntry entry = new PageEntry();
            entry.theme = parcel.readParcelable(Theme.class.getClassLoader());
            Parcelable[] pars = parcel.readParcelableArray(TileEntry.class.getClassLoader());
            if(pars!=null) {
                entry.tileEntrys = Arrays.copyOf(pars, pars.length, TileEntry[].class);
            }
            return entry;
        }

        @Override
        public PageEntry[] newArray(int i) {
            return new PageEntry[i];
        }
    };


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public String toString() {
        return "PageEntry{" +
                "theme=" + theme +
                ", tileEntrys=" + Arrays.toString(tileEntrys) +
                '}';
    }
}
